package net.learn2develop.GPS;

import net.learn2develop.DB.DatabaseHelper;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class FavoritesList extends ListActivity {

	private Place[] listItems = null;
	protected ListAdapter adapter;
	protected Cursor cursor;

	 /** Called when the activity is first created. */
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.favorites);
       
        SQLiteDatabase db = (new DatabaseHelper(getBaseContext())).getWritableDatabase();
        Cursor cursor = db.rawQuery("SELECT _id, nameplace, latitud, longitud, address FROM places WHERE favorite LIKE 1", new String[]{});
        
        int i = 0;
		if(cursor.moveToFirst()){
			listItems = new Place [cursor.getCount()];
			do{
				listItems[i] = new Place(cursor.getString(cursor.getColumnIndex("nameplace")), cursor.getString(cursor.getColumnIndex("latitud")), cursor.getString(cursor.getColumnIndex("longitud")),
						cursor.getString(cursor.getColumnIndex("address")), "", "", "", "", "", "", 0, 0, 0,Integer.valueOf(cursor.getString(cursor.getColumnIndex("_id"))));
				i++;
			} while (cursor.moveToNext());
			adapter = new PlaceAdapter(FavoritesList.this,android.R.layout.simple_list_item_1,listItems);
			setListAdapter(adapter);
			ListView lv = getListView();
	        lv.setTextFilterEnabled(true);
		}
		
		if (cursor != null && !cursor.isClosed()) {
	         cursor.close();
		}	
    }
	
	protected  void onResume(){
		super.onResume();
        setContentView(R.layout.favorites);
       
        SQLiteDatabase db = (new DatabaseHelper(getBaseContext())).getWritableDatabase();
        Cursor cursor = db.rawQuery("SELECT _id, nameplace, latitud, longitud, address FROM places WHERE favorite LIKE 1", new String[]{});
        
        int i = 0;
		if(cursor.moveToFirst()){
			listItems = new Place [cursor.getCount()];
			do{
				listItems[i] = new Place(cursor.getString(cursor.getColumnIndex("nameplace")), cursor.getString(cursor.getColumnIndex("latitud")), cursor.getString(cursor.getColumnIndex("longitud")),
						cursor.getString(cursor.getColumnIndex("address")), "", "", "", "", "", "", 0, 0, 0,Integer.valueOf(cursor.getString(cursor.getColumnIndex("_id"))));
				i++;
			} while (cursor.moveToNext());
			adapter = new PlaceAdapter(FavoritesList.this,android.R.layout.simple_list_item_1,listItems);
			setListAdapter(adapter);
			ListView lv = getListView();
	        lv.setTextFilterEnabled(true);
		}
		
		if (cursor != null && !cursor.isClosed()) {
	         cursor.close();
		}
	}
	
	public void onListItemClick(ListView parent, View view, int position, long id) {
		Intent intent = new Intent(FavoritesList.this, PlaceInMap.class);
   		Place place = (Place) adapter.getItem(position);
    	intent.putExtra("PLACE_ID", place.getIdPlace());
    	startActivity(intent);
	}
	
	private class PlaceAdapter extends ArrayAdapter<Place> {

        private Place[] places;

        public PlaceAdapter(Context context, int textViewResourceId, Place[] places) {
                super(context, textViewResourceId, places);
                this.places = places;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
                View v = convertView;
                if (v == null) {
                    LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                    v = vi.inflate(R.layout.favorite_row, null);
                }
                Place o = places[position];
                if (o != null) {
                        TextView tt = (TextView) v.findViewById(R.id.toptext);
                        TextView bt = (TextView) v.findViewById(R.id.bottomtext);
                        if (tt != null) {
                              tt.setText("Name: "+o.getName());                            }
                        if(bt != null){
                              bt.setText("Address: "+ o.getAddress());
                        }
                }
                return v;
        }
	}
	
	public boolean onCreateOptionsMenu(Menu menu){

    	menu.add(0,1,0,"Show All").setIcon(android.R.drawable.ic_menu_mapmode);
    	return true;

   }
    
    public boolean onOptionsItemSelected (MenuItem item){

    	switch (item.getItemId()){
    	case 1:
    		startActivity(new Intent(getBaseContext(), AllFavorites.class));
    	return true;
    	}
    	return false;
    }
}
